package com.hs.jhzd.controller;

import com.hs.jhzd.entity.Prisoner;
import com.hs.jhzd.entity.SwipeRecord;
import com.hs.jhzd.entity.dto.SwipeRecordDto;
import com.hs.jhzd.repository.PrisonerRepository;
import com.hs.jhzd.repository.SwipeRecordRepository;
import com.hs.jhzd.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/swipe")
public class SwipeController {
    @Autowired
    private SwipeRecordRepository repository;

    @Autowired
    private PrisonerRepository prisonerRepository;

    @Value("${jsbh}")
    private String jsbh;

    /**
     * 点名刷卡.
     *
     * @param swipeRecord 刷卡记录信息
     * @return 是否成功
     */
    @RequestMapping("/rollcall")
    @ResponseBody
    public boolean rollcall(SwipeRecord swipeRecord) {
        swipeRecord.setJsbh(jsbh);
        swipeRecord.setJSMC(jsbh);
        swipeRecord.setSklx(SwipeRecord.SWIPE_TYPE_PRISONER_ROLLCALL);

        repository.save(swipeRecord);
        return true;
    }


    /**
     * 值班刷卡.
     *
     * @param swipeRecord 刷卡记录信息
     * @return 是否成功
     */
    @RequestMapping("/shift")
    @ResponseBody
    public boolean shift(SwipeRecord swipeRecord) {
        swipeRecord.setJSMC(jsbh);
        swipeRecord.setJsbh(jsbh);
        swipeRecord.setSklx(SwipeRecord.SWIPE_TYPE_PRISONER_SHIFT);
        repository.save(swipeRecord);
        return true;
    }

    /**
     * 获取点名结果.
     *
     * @param prisonId  监所编号
     * @param dormCodes 监舍号
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param swipe     刷卡
     * @return 点名结果列表
     */
    @RequestMapping("/rollcallResult")
    @ResponseBody
    public List<SwipeRecordDto> rollcallResult(String jsbh, String[] jshs,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                               @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, Boolean swipe) {

        if (startTime == null) {
            startTime = DateUtils.getStartOfDay(new Date());
        }
        if (endTime == null) {
            endTime = DateUtils.getEndOfDay(new Date());
        }
        List<SwipeRecordDto> swipeRecordDtos = new ArrayList<>();
        if (jshs == null || jshs.length < 1) {
            List<SwipeRecord> swipeRecords = repository.findSwipeRecordByJsbh(jsbh, startTime, endTime,
                    SwipeRecord.SWIPE_TYPE_PRISONER_ROLLCALL);
            List<Prisoner> prisoners = prisonerRepository.findAll();
            if (!CollectionUtils.isEmpty(prisoners)) {
                swipeRecordDtos.addAll(countRollcallPrisoner(prisoners, swipeRecords));
            }
        } else {
            for (String jsh : jshs) {
                List<SwipeRecord> swipeRecords = repository.findSwipeRecord(jsbh, jsh, startTime, endTime,
                        SwipeRecord.SWIPE_TYPE_PRISONER_ROLLCALL);
                List<Prisoner> prisoners = prisonerRepository.findByJshAndRybj(jsh, "0");
                if (CollectionUtils.isEmpty(prisoners)) {
                    continue;
                }
                swipeRecordDtos.addAll(countRollcallPrisoner(prisoners, swipeRecords));
            }
        }
        if (swipe != null) {
            List<SwipeRecordDto> swipeDtos = new ArrayList<>();
            if (swipe) {
                for (SwipeRecordDto swipeRecordDto : swipeRecordDtos) {
                    if (swipeRecordDto.getSwipe()) {
                        swipeDtos.add(swipeRecordDto);
                    }
                }
                return swipeDtos;
            } else {
                for (SwipeRecordDto swipeRecordDto : swipeRecordDtos) {
                    if (!swipeRecordDto.getSwipe()) {
                        swipeDtos.add(swipeRecordDto);
                    }
                }
                return swipeDtos;
            }

        } else {
            return swipeRecordDtos;
        }
    }

    @RequestMapping("/rollcall/admin/main")
    public String rollcallMainPage(Model model, String[] jshs,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                   @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, Boolean swipe) {
        List<SwipeRecordDto> swipeRecordDtos = rollcallResult(jsbh, jshs, startTime, endTime, swipe);
        model.addAttribute("jsbh", jsbh);
        model.addAttribute("results", swipeRecordDtos);
        return "rollcall-list";
    }


    @RequestMapping("/shiftResult")
    @ResponseBody
    public List<SwipeRecordDto> shiftResult(String jsbh, String[] jshs,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, Boolean swipe) {
        List<SwipeRecordDto> swipeRecordDtos = new ArrayList<>();
        if (startTime == null) {
            startTime = DateUtils.getStartOfDay(new Date());
        }
        if (endTime == null) {
            endTime = DateUtils.getEndOfDay(new Date());
        }
        if (jshs == null || jshs.length < 1) {
            List<SwipeRecord> swipeRecords = repository.findSwipeRecordByJsbh(jsbh, startTime, endTime,
                    SwipeRecord.SWIPE_TYPE_PRISONER_SHIFT);
            List<Prisoner> prisoners = prisonerRepository.findAll();
            if (!CollectionUtils.isEmpty(prisoners)) {
                swipeRecordDtos.addAll(countRollcallPrisoner(prisoners, swipeRecords));
            }
        } else {
            for (String jsh : jshs) {
                List<SwipeRecord> swipeRecords = repository.findSwipeRecord(jsbh, jsh, startTime, endTime,
                        SwipeRecord.SWIPE_TYPE_PRISONER_SHIFT);
                List<Prisoner> prisoners = prisonerRepository.findByJshAndRybj(jsh, "0");
                if (CollectionUtils.isEmpty(prisoners)) {
                    continue;
                }
                swipeRecordDtos.addAll(countRollcallPrisoner(prisoners, swipeRecords));
            }
        }
        if (swipe != null) {
            List<SwipeRecordDto> swipeDtos = new ArrayList<>();
            if (swipe) {
                for (SwipeRecordDto swipeRecordDto : swipeRecordDtos) {
                    if (swipeRecordDto.getSwipe()) {
                        swipeDtos.add(swipeRecordDto);
                    }
                }
                return swipeDtos;
            } else {
                for (SwipeRecordDto swipeRecordDto : swipeRecordDtos) {
                    if (!swipeRecordDto.getSwipe()) {
                        swipeDtos.add(swipeRecordDto);
                    }
                }
                return swipeDtos;
            }

        } else {
            return swipeRecordDtos;
        }
    }

    @RequestMapping("/shift/admin/main")
    public String shiftMainPage(Model model, String[] jshs,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                                @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime, Boolean swipe) {
        List<SwipeRecordDto> swipeRecordDtos = shiftResult(jsbh, jshs, startTime, endTime, swipe);
        model.addAttribute("jsbh", jsbh);
        model.addAttribute("results", swipeRecordDtos);
        return "shift-list";
    }

    private List<? extends SwipeRecordDto> countRollcallPrisoner(List<Prisoner> prisoners,
                                                                 List<SwipeRecord> swipeRecords) {
        List<SwipeRecordDto> swipeRecordDtos = new ArrayList<>();

        for (Prisoner prisoner : prisoners) {
            boolean flag = false;
            SwipeRecordDto dto = new SwipeRecordDto();
            dto.setJsh(prisoner.getJsh());
            dto.setPrisonerName(prisoner.getName());
            if (CollectionUtils.isEmpty(swipeRecords)) {
                continue;
            }
            for (SwipeRecord swipeRecord : swipeRecords) {
                if (StringUtils.equals(swipeRecord.getRybh(), prisoner.getRybh())) {
                    flag = true;
                    dto.setSwipeTime(swipeRecord.getSksj());
                    break;
                }
            }
            dto.setSwipe(flag);
            swipeRecordDtos.add(dto);
        }
        return swipeRecordDtos;
    }
}
